/* Given N line segments, the task is to find out a ray which starts from the
 * origin and intersects with as many line segments as possible. To find it,
 * we first sort all end points by their angle from the origin, which is in
 * the range of [0, 360) degrees. We then gradually rotates the ray from 0
 * degrees to 360 degrees to count the number of intersections.
 */

#include <stdio.h>
#include <utility>
#include <algorithm>

#define MAX_SEGMENTS 1500
struct Segment { int x1, y1, x2, y2; };
static Segment s[MAX_SEGMENTS]; /* line segments */
static int n;                   /* number of segments */

static void read_segments()
{
    scanf("%d", &n);
    for (int i = 0; i < n; i++)
        scanf("%d %d %d %d", &s[i].x1, &s[i].y1, &s[i].x2, &s[i].y2);

    int x0, y0;
    scanf("%d %d", &x0, &y0);
    for (int i = 0; i < n; i++)
    {
        s[i].x1 -= x0;
        s[i].y1 -= y0;
        s[i].x2 -= x0;
        s[i].y2 -= y0;
    }
}

struct Fraction
{
    int p, q; 
    Fraction() : p(0), q(1) { }
    Fraction(int num, int denom) : p(num), q(denom) { }
};

static bool operator < (const Fraction &a, const Fraction &b)
{
    return (long long)a.p*b.q < (long long)b.p*a.q;
}

/* Returns a fraction between [0, 4) to represent the angle t of a point 
 * relative to the origin. The fraction is computed as below:
 * - Quadrant I:   sin^2(t)     : 0 -> 1
 * - Quadrant II:  2 - sin^2(t) : 1 -> 2
 * - Quadrant III: 2 + sin^2(t) : 2 -> 3
 * - Quadrant IV:  4 - sin^2(t) : 3 -> 4
 */
static Fraction point_angle(int x, int y)
{
    int y2 = y*y, d2 = x*x + y*y;
    if (x == 0 && y == 0)       /* origin */
        return Fraction();
    else if (x >= 0 && y >= 0)  /* quadrant I */
        return Fraction(y2, d2);
    else if (x <= 0 && y >= 0)  /* quadrant II */
        return Fraction(2*d2-y2, d2);
    else if (x <= 0 && y >= 0)  /* quadrant III */
        return Fraction(2*d2+y2, d2);
    else                        /* quadrant IV */
        return Fraction(4*d2-y2, d2);
}

/* points[i] stores the attributes of an end point, where 'first' is the point
 * angle relative to the origin, and 'second' or '~second' (whichever is >= 0)
 * is the segment index. If 'second' < 0, the point is the entry point of the
 * segment when the ray rotates counter-clockwise starting from y = 0, x > 0;
 * if 'second' >= 0, the point is the exit point of the segment.
 */
static std::pair<Fraction,int> points[MAX_SEGMENTS*2];

// Return the maximum number of segments a ray from the origin can intersect.
static int solve()
{
    int num_crossed = 0;
    int num_points = 0;

    /* Process each line segment. */
    for (int i = 0; i < n; i++)
    {
        int x1 = s[i].x1, y1 = s[i].y1, x2 = s[i].x2, y2 = s[i].y2;

        /* If the line segment passes through the origin, it always intersects
         * a ray from the origin, so increment the crossing counter and don't
         * process it further.
         */
        if (x1*y2 == x2*y1 && x1*x2 <= 0 && y1*y2 <= 0)
        {
            ++num_crossed;
            continue;
        }

        /* Compute the point angle of each end-point. */
        Fraction angle1 = point_angle(x1, y1), angle2 = point_angle(x2, y2);

        /* If the line segment is part of the initial ray (y = 0, x > 0),
         * add both end-points which have the same entry or exit priority.
         */
        if (y1 == 0 && y2 == 0 && x1 > 0 && x2 > 0)
        {
            points[num_points++] = std::make_pair(angle1, ~i);
            points[num_points++] = std::make_pair(angle2, i);
            continue;
        }

        /* If the line segment intersects the initial ray (y = 0, x > 0)
         * and one of the end-points is below the x-axis, then the end-point
         * with smaller angle is the exit point, and the end-point with larger
         * angle is the entry point. Mark the intersection and increment the
         * counter.
         */
        if ((y1 < 0 && y2 >= 0 || y2 < 0 && y1 >= 0) &&
            (long long)(x1*y2-x2*y1)*(y2-y1) > 0)
        {
            points[num_points++] = std::make_pair(std::max(angle1,angle2),~i);
            points[num_points++] = std::make_pair(std::min(angle1,angle2), i);
            ++num_crossed;
        }
        else /* segment does not intersect initial ray */
        {
            points[num_points++] = std::make_pair(std::min(angle1,angle2),~i);
            points[num_points++] = std::make_pair(std::max(angle1,angle2), i);            
        }
    }
    
    /* Sort the end-points by their angle; for end-points of the same angle,
     * entry points are placed before exit points (which is critical).
     */
    std::sort(points, points + num_points);

    /* Rotate the ray counter-clockwise to meet each end point. */
    int max_crossed = num_crossed;
    for (int i = 0; i < num_points; i++)
    {
        int k = points[i].second; /* segment index or ~segment index */
        if (k < 0)  /* this is an entry point */
            ++num_crossed;
        else        /* this is an exit point */
            --num_crossed;
        if (num_crossed > max_crossed)
            max_crossed = num_crossed;
    }
    return max_crossed;
}

int main()
{
    int T;
    scanf("%d", &T);
    for (int t = 0; t < T; t++)
    {
        read_segments();
        printf("%d\n", solve());
    }
    return 0;
}